/*
    2023/1/23 code by Electrolux
    侧边栏事件和动效示例
    解决了上一个版本的问题
*/

var storage = storages.create("侧边栏"); //创建本地储存
//var Task=require("task.js");
var img_url = "file://img/home.jpg"

importClass(android.animation.ObjectAnimator)
var img_setting = "file://img/setting.jpg"
var img_clear = "file://img/clear.jpg"
var img_box = "file://img/box.jpg"
main();
var status = false;
function main() {
    window = floaty.rawWindow(
        <frame margin="0 0 0 0" width="110" h="170" gravity="left">


            <frame id="drawer" margin="0 0 0 0" h="160" width="*" >
                <img id="ui_setting" src="{{img_setting}}" margin="0 0 0 0" w="40" alpha="0.8" circle="true" />
                <img id="ui_clear" src="{{img_clear}}" margin="0 -3 0 0" w="40" alpha="0.8" circle="true" />
                <img id="ui_close" src="{{img_box}}" margin="0 -0 0 0" w="40" h="*" alpha="0.8" circle="true" />
            </frame>
            <img id="ui_controller" src="{{img_url}}" w="50" h="50" marginTop="55" alpha="0.8" circle="true" />
        </frame>
    );
    window.setPosition(0, device.height / 3);
    window.exitOnClose();
    setInterval(() => { }, 1000);
    //隐藏
    window.drawer.visibility = 8;
    var x = 0, count = 0,
        y = 0;
    var windowX, windowY;
    var clear_img_top = 0
    var clear_img_left = 0

    var close_img_top = 0
    var close_img_left = 0
    // setting
    var setting_img_top = 0
    var setting_img_left = 0
    window.ui_controller.setOnTouchListener(function (view, event) {
        switch (event.getAction()) {
            case event.ACTION_DOWN:
                //抽屉状态() 
                x = event.getRawX();
                y = event.getRawY();
                windowX = window.getX();
                windowY = window.getY();
                downTime = new Date().getTime();
                return true;
            case event.ACTION_MOVE:
                window.setPosition(windowX + (event.getRawX() - x),
                    windowY + (event.getRawY() - y));
                return true;
            case event.ACTION_UP:
                if (Math.abs(event.getRawY() - y) < 5 && Math.abs(event.getRawX() - x) < 5) {
                    //toastLog(233)
                    swiperDrawer();
                }
                return true;
        }
        return true;
    });
    function swiperDrawer() {
        if (window.drawer.visibility == 8) {
            window.drawer.visibility = 0
            if (status == false) {
                for (let i = 0; i < 100; i++) {
                    setTimeout(() => {
                        setting_img_top = setting_img_top - 1.5
                        setting_img_left = setting_img_left + 0.6
                        window.ui_setting.translationY = setting_img_top
                        window.ui_setting.translationX = setting_img_left
                    }, i * 3)
                }
                setTimeout(() => {
                    for (let i = 0; i < 100; i++) {
                        setTimeout((i) => {
                            clear_img_left = clear_img_left + 1.8
                            clear_img_top = clear_img_top + 0.08
                            window.ui_clear.translationY = clear_img_top
                            window.ui_clear.translationX = clear_img_left
                        }, i * 3)
                    }
                }, 125)

                setTimeout(() => {
                    for (let i = 0; i < 100; i++) {
                        setTimeout((i) => {
                            close_img_top = close_img_top + 1.6
                            close_img_left = close_img_left + 0.6
                            window.ui_close.translationY = close_img_top
                            window.ui_close.translationX = close_img_left
                        }, i * 3)
                    }

                }, 250)

                status = true
            }
        // 关闭
        } else {
            if (status == true) {
                for (let i = 0; i < 100; i++) {
                    setTimeout((i) => {
                        setting_img_top = setting_img_top + 1.5
                        setting_img_left = setting_img_left - 0.6
                        window.ui_setting.translationY = setting_img_top
                        window.ui_setting.translationX = setting_img_left

                        close_img_top = close_img_top - 1.6
                        close_img_left = close_img_left - 0.6
                        window.ui_close.translationY = close_img_top
                        window.ui_close.translationX = close_img_left

                        clear_img_left = clear_img_left - 1.8
                        clear_img_top = clear_img_top - 0.08
                        window.ui_clear.translationY = clear_img_top
                        window.ui_clear.translationX = clear_img_left

                        count = count + 1
                        //window.ui_clear.rotation=count
                        window.drawer.alpha = (1 - count / 100)
                    }, i * 3)

                }
                setTimeout(() => {
                    window.drawer.alpha = 1
                    window.drawer.visibility = 8
                    count = 0
                }, 800)
                status = false
            }
        }
    }
    window.ui_close.setOnTouchListener(function (view, event) {
        if (event.getAction() == event.ACTION_UP) {
            //window.setPosition(50, device.height / 3);
            window.disableFocus()
            toastLog("退出应用成功")
            //window.drawer.visibility = 8
            window.close();
            exit();

        }
        return true;
    });
    window.ui_clear.click(() => {
        //toast("跳转中")
        var i = dialogs.singleChoice("请问要去哪里呢", ["xx12后台", "机器人控制", "测试系统"], 0).then((i) => {

            if (i != -1) {
                toast(i)
                app.openUrl("https://electrolux.gitee.io/front-supper-know-blog/")
            }
        })
    })
    //运行按钮事件
    window.ui_setting.setOnTouchListener(function (view, event) {
        if (event.getAction() == event.ACTION_UP) {
            //window.setPosition(50, device.height / 3);
            window.disableFocus();
            var releaseNotes = "版本 v0.0.1\n" +
                "更新日志:\n" +
                "* 新增 若干Bug\n";
            dialogs.build({
                title: "Electrolux插件发现新版本",
                content: releaseNotes,
                positive: "立即下载",
                negative: "取消",
                neutral: "到浏览器下载"
            }).on("neutral", () => {
                app.openUrl("https://www.autojs.org");
            }).show();
            /* if (window.ui_setting.text() == "开始") {
                 window.ui_setting.text("暂停");
                 console.log("开始运行悬浮窗");
 
                 var main = threads.start(function() {
                     device.keepScreenOn()
                     //运行脚本
                     // Task.runTask();
                 })
 
                 //两秒不点击暂停，则隐藏抽屉
                 setTimeout(function() {
                     if (window.ui_setting.text() == "暂停") {
                         抽屉状态()
                     }
                 }, 3000)
 
                 //监控运行还是暂停
           
             } else {
                 状态 = false;
                 window.ui_运行.text("开始");
                 toastLog("开始暂停...");
             }*/
        }
        return true;
    });
}